function getxmlHttpRequest(servletName,formname,responsediv,responsemsg) {
    var xmlhttp = false;
    var x = this;
    if (window.XMLHttpRequest) {
        x.xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        x.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    x.xmlhttp.open('POST', servletName, true);
    x.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
    updatepage(responsemsg,responsediv);
    x.xmlhttp.onreadystatechange = function() {
        if (x.xmlhttp.readyState == 4) {
            updatepage(x.xmlhttp.responseText,responsediv);
        }
    };
    x.xmlhttp.send(getquerystring(formname));
}

function getXmlHttpRequestUpdatePoll()
{
	var responsemsg = 'wait', responsediv = 'message';
	var sampleText = document.getElementById("companyCampaignID").value;
	var sampleFile = document.getElementById("fileUploadName").files[0];
	
	var formdata = new FormData();
	formdata.append("id", sampleText);
	formdata.append("file", sampleFile);	        		
	
	var xhr;
	    if (window.XMLHttpRequest) {
	    	xhr = new XMLHttpRequest();
	    }
	    else if (window.ActiveXObject) {
	    	xhr = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	
	xhr.open("POST","executeAction?action=uploadPoll", true);
	updatepageFileUpload(responsemsg,responsediv);
	xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
        	updatepageFileUpload(xhr.responseText,responsediv);
        }
    };
	xhr.send(formdata);
}

function getxmlHttpRequestFileUpload(servletName,formname,responsediv,responsemsg) {
    var xmlhttp = false;
    var x = this;
    var filesToBeUploaded = document.getElementById("fileUploadName");
    var file = filesToBeUploaded.files[0];
    if (window.XMLHttpRequest) {
        x.xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
    	x.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    x.xmlhttp.open('POST', servletName, true);
    x.xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
    updatepageFileUpload(responsemsg,responsediv);
    var formData = new FormData();
    formData.append("thefile", file);
    x.xmlhttp.send(formData);    
    x.xmlhttp.onreadystatechange = function() {
        if (x.xmlhttp.readyState == 4) {
            updatepageFileUpload(x.xmlhttp.responseText,responsediv);
        }
    };
    x.xmlhttp.send(getquerystring(formname));
}

function getquerystring(formname) {
    var form = document.forms[formname];
    var qstr = "";
    
    function GetElemValue(id, value) {    	
        qstr += (qstr.length > 0 ? "&" : "")
            + escape(id).replace(/\+/g, "%2B") + "="
            + escape(value ? value : "").replace(/\+/g, "%2B");
			+ escape(value ? value : "").replace(/\n/g, "%0D");
    }
	
	var elemArray = form.elements;
	
	var i = 0;
	while(i < elemArray.length)
	{
		var element = elemArray[i];
        var elemType = element.type;
        if (elemType != null)
        {
        	var elemID = element.id;
            if (elemID) {
                if (elemType.toUpperCase() == "TEXT"
                        || elemType.toUpperCase() == "TEXTAREA"
                        || elemType.toUpperCase() == "HIDDEN"
                        || elemType.toUpperCase() == "FILE")
                	{
                		if(elemID != "xl_aux2")
                		{
                			GetElemValue(elemID, element.value);	
                		}            		                   		
                	}
                else if (elemType.toUpperCase() == "CHECKBOX" && element.checked)
                	{
                		GetElemValue(elemID, 
                            element.value ? element.value : "On");
                	}
                else if (elemType.toUpperCase().indexOf("SELECT") != -1)
            	{
                	for (var j = 0; j < element.options.length; j++) {
                        var option = element.options[j];
                        if (option.selected)
                        {
                        	GetElemValue(elemID,option.value ? option.value : option.text);
                        }
                    }
            	}
            }	
        }
		i++;
	}
    return qstr;
}

function updatepage(str,responsediv){
	if(str.toUpperCase() == "WAIT")
	{
		centerSavePopup();
		loadSavePopup();
		document.getElementById("saveComment").innerHTML = "<h1>Guardando...</h1>";
		document.getElementById("saveComment").setAttribute("class", "save");
		document.getElementById("SaveArea").innerHTML = "";
	}
	else
	{
		var paramsSplit = str.split("~");
		
		var msgeType = "";
		var msge = "";
		var status = "";
		var params = "";
		var pollStatus = "";
		
		if(paramsSplit.length == 2)
		{
			msgeType = paramsSplit[0];
			msge = paramsSplit[1];
		}
		else 
		{
			msgeType = paramsSplit[0];
			msge = paramsSplit[1];
			status = paramsSplit[2];
			params = paramsSplit[3];
			pollStatus = paramsSplit[4];
		}
		
		if(msgeType == "1")
		{
			document.getElementById("saveComment").innerHTML = "<h1>" + msge + "</h1>";
			document.getElementById("saveComment").setAttribute("class", "error");
			document.getElementById("SaveArea").innerHTML = "<input class=\"buttonsGreen\" onclick=\"disableSavePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonPopupSave\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		else if(msgeType == "2")
		{
			document.getElementById("saveComment").innerHTML = "<h1>" + msge + "</h1>";
			document.getElementById("saveComment").setAttribute("class", "alerta");
			document.getElementById("SaveArea").innerHTML = "<input class=\"buttonsGreen\" onclick=\"disableSavePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonPopupSave\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		else if(msgeType == "3")
		{
			document.getElementById("saveComment").innerHTML = "<h1>" + msge + "</h1>";
			document.getElementById("saveComment").setAttribute("class", "info");
			document.getElementById("SaveArea").innerHTML = "<input class=\"buttonsGreen\" onclick=\"disableSavePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonPopupSave\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		else if(msgeType == "4")
		{
			document.getElementById("saveComment").innerHTML = "<h1>" + msge + "</h1>";
			document.getElementById("saveComment").setAttribute("class", "exito");
			document.getElementById("SaveArea").innerHTML = "<input class=\"buttonsGreen\" onclick=\"disableSavePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonPopupSave\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		if(status != null && status.length > 0 && params != null && params.length > 0 && pollStatus != null && pollStatus.length > 0)
		{
			if(pollStatus == 0)
			{
				changeFormStatus(status,params);
			}
			else
			{
				changePollFormStatus(status,params,pollStatus);
			}
		}
	}
}

function updatepageFileUpload(str,responsediv){
	
	document.getElementById("popupClose").style.display = "none";
	document.getElementById("uploadArea").style.display = "none";
	document.getElementById("messageArea").style.display = "inline";	
	
	if(str.toUpperCase() == "WAIT")
	{
		document.getElementById("saveComment").innerHTML = "<h2>Guardando...</h2>";
		document.getElementById("saveComment").setAttribute("class", "save");
		document.getElementById("SaveArea").innerHTML = "";
	}
	else
	{
		var paramsSplit = str.split("~");
		
		var msgeType = "";
		var msge = "";
		
		if(paramsSplit.length == 2)
		{
			msgeType = paramsSplit[0];
			msge = paramsSplit[1];
		}		
		
		if(msgeType == "1")
		{
			document.getElementById("saveComment").innerHTML = "<h2>" + msge + "</h2>";
			document.getElementById("saveComment").setAttribute("class", "error");
			document.getElementById("SaveArea").innerHTML = "<input class=\"fg-button ui-state-default ui-corner-all\" onclick=\"initPopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonOK\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		else if(msgeType == "2")
		{
			document.getElementById("saveComment").innerHTML = "<h2>" + msge + "</h2>";
			document.getElementById("saveComment").setAttribute("class", "alerta");
			document.getElementById("SaveArea").innerHTML = "<input class=\"fg-button ui-state-default ui-corner-all\" onclick=\"disablePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonOK\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		else if(msgeType == "3")
		{
			document.getElementById("saveComment").innerHTML = "<h2>" + msge + "</h2>";
			document.getElementById("saveComment").setAttribute("class", "info");
			document.getElementById("SaveArea").innerHTML = "<input class=\"fg-button ui-state-default ui-corner-all\" onclick=\"disablePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonOK\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
		else if(msgeType == "4")
		{
			document.getElementById("titleArea").innerHTML = "<h1>Proceso finalizado</h1>";
			document.getElementById("saveComment").innerHTML = "<h2>" + msge + "</h2>";
			document.getElementById("saveComment").setAttribute("class", "exito");
			document.getElementById("SaveArea").innerHTML = "<input class=\"fg-button ui-state-default ui-corner-all\" onclick=\"disablePopup();\" align=\"right\" id=\"buttonPopupSave\" name=\"buttonOK\" style=\"width: 140px;\" type=\"button\" value=\"Aceptar\">";
		}
	}
}